import { InjectionKey } from 'vue'
import { createStore, Store, useStore as baseUseStore } from 'vuex'
import storeCategoriesModule from './goods-module/categories-module/store-categories-module'
import storeModeleProductList from './goods-module/productList-module/store-module-productList'
import storeRightsModule from './rights-module/store-rights-module'
import productParamsModule from './goods-module/params-module/store-params-module'
import storeOrdersModule from './orders-module/store-orders-module'
import storeUsersModule from './users-module/store-users-module'
import { postFn, getFn } from './store_actions_axiosFn'
import { GlobalDataProps, AllStateType } from './storeType'

export default createStore<GlobalDataProps>({
  state: {
    user: { state: false, token: '' },// 用户数据
    menuList: [],// 左选择栏数据
  },
  mutations: {
    Login (state, data) {
      state.user = data.data.data
    },
    getMenuList (state, data) {
      if (data.status === 200) {
        state.menuList = data.data.data
      }
    },
    postGoodsData (state, data) {
      console.log(state, '--------', data)
    }
  },
  actions: {
    postUserData (context, userdata) {
      const { userName, password } = userdata
      return postFn('/login', {username: userName, password}, 'Login', context.commit)
    },
    getMenuList ({ commit }) {
      return getFn('/menus', 'getMenuList', commit)
    },
    postGoodsData ({ commit }, goodsData) {
      return postFn('/goods', goodsData, 'postGoodsData', commit)
    },
  },
  modules: {
    productList: storeModeleProductList,
    paramsModule: productParamsModule,
    categoriesModule: storeCategoriesModule,
    ordersModule: storeOrdersModule,
    rightsModule: storeRightsModule,
    usersModule: storeUsersModule
  }
})

export const key: InjectionKey<Store<GlobalDataProps>> = Symbol('vue-store')

export function useStore<T = AllStateType>() {
  return baseUseStore<T>(key)
}